const router = require('koa-router')()
const IndustryModel = require('../models/industry')

router.prefix('/api')


//获取所有大客户行业
router.get('/getIndustry',async (ctx) => {
    ctx.status = 200

    let res = await IndustryModel.find({},{job:true,_id:false})

    let res1 = []

    res.forEach((item)=>{
        res1.push(item.job)
    })


    ctx.body = {
        code:200,
        data:res1
    }
})

//大客户行业添加
router.post('/addIndustry',async (ctx) => {
    ctx.status = 200

    let {job} = ctx.request.body

    let res = await IndustryModel.find({})

    let id = 1
    if(res.length != 0){
        id = res[res.length-1].id+1
    }

    await IndustryModel.create({id,job})

    ctx.body = {
        code:200,
        msg:"行业添加成功"
    }
})

//大客户行业编辑
router.post('/editIndustry',async (ctx) => {
    ctx.status = 200

    let {id,job} = ctx.request.body

    let res = await IndustryModel.findOneAndUpdate({id}, {$set:{job}})

    if(res.job == job){
        ctx.body = {
            code:200,
            msg:"修改行业未改变"
        }
    }else {
        ctx.body = {
            code:200,
            msg:"修改行业成功"
        }
    }

})

//大客户行业删除
router.post('/deleteIndustry',async (ctx) => {
    ctx.status = 200

    let {id} = ctx.request.body

    let res = await IndustryModel.findOneAndDelete({id})

    if(res != null){
        ctx.body = {
            code:200,
            msg:"删除成功"
        }
    }else {
        ctx.body = {
            code:200,
            msg:"删除失败"
        }
    }

})



module.exports = router
